<template>
    <div>
        <div class="wrapper">
            <div
                style="margin: 200px auto; background: linear-gradient(rgb(238, 232, 232),rgb(233, 215, 215));width: 350px; height: 300px; padding: 20px; border-radius: 10px">
                <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>登 录</b></div>
                <el-form :model="customer" :rules="rules" ref="customerForm">
                    <el-form-item prop="username">
                        <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user"
                            v-model="customer.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password
                            v-model="customer.password"></el-input>
                    </el-form-item>
                    <el-form-item style="margin: 10px 0; text-align: right">
                        <el-button type="warning" size="small" autocomplete="off" @click="register">注册</el-button>
                        <el-button type="primary" size="small" autocomplete="off" @click="login">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { setRoutes } from "@/router";

export default {
    name: "Login",
    data() {
        return {
            customer: {},
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    mounted() {
        var customer = localStorage.getItem("customer")
        if (customer != null) {
            console.log("当卡customer==》", customer)
            this.$router.push("/front")
        }
    },
    methods: {
        login() {
            //登录
            this.$refs['customerForm'].validate((valid) => {
                if (valid) {
                    this.request.post("/customer/login", {
                        username: this.customer.username,
                        password: this.customer.password
                    }).then(res => {
                        console.log(res)
                        if (res.code == 200) {
                            localStorage.setItem("customer", JSON.stringify(res.data))  // 存储客户信息到浏览器
                            this.$message.success("登录成功")
                            this.$router.go(0)
                        } else {
                            this.$message.error(res.msg)
                        }
                    })
                }
            });
        },
        register() {
            this.$router.push("/front/register")
        }
    }
}
</script>

<style>
.wrapper {
   
    background-image: url(../../assets/bj.jpeg);
    background-repeat: no-repeat;
    background-size: 100% 100%;
   
    overflow: hidden;
}
</style>